package com.mashiro.system.service.impl;

import com.mashiro.common.core.domain.entity.SysUser;
import com.mashiro.common.utils.DateUtils;
import com.mashiro.common.utils.SecurityUtils;
import com.mashiro.system.domain.MoeMessage;
import com.mashiro.system.domain.SysNotice;
import com.mashiro.system.domain.status.MoeMessageStatus;
import com.mashiro.system.domain.types.MoeMessageType;
import com.mashiro.system.mapper.MoeMessageMapper;
import com.mashiro.system.mapper.SysNoticeMapper;
import com.mashiro.system.mapper.SysUserMapper;
import com.mashiro.system.service.ISysNoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.mashiro.common.utils.SecurityUtils.getUsername;

/**
 * 公告 服务层实现
 *
 * @author mashiro
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Resource
    private SysNoticeMapper noticeMapper;
    @Resource
    private MoeMessageMapper moeMessageMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        Long userId = SecurityUtils.getUserId();
        Date date = DateUtils.getNowDate();
        List<SysUser> userList = sysUserMapper.selectUserList(new SysUser());
        // 站内消息配信
        MoeMessage message = new MoeMessage();
        message.setSenderId(userId);
        message.setTitle(notice.getNoticeTitle());
        message.setContent(notice.getNoticeContent());
        message.setType(MoeMessageType.NOTIFICATION.getValue());
        message.setCreateTime(date);
        message.setCreateBy(getUsername());
        notice.setCreateTime(date);
        notice.setCreateBy(getUsername());
        userList.forEach((user)->{
            message.setReceiverId(user.getUserId());
            moeMessageMapper.insertMoeMessage(message);
        });
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        Date date = DateUtils.getNowDate();
        String user = getUsername();
        List<MoeMessage> messageList = moeMessageMapper.selectMoeMessageListByNoticeId(notice.getNoticeId());
        messageList.forEach((message)->{
            message.setTitle(notice.getNoticeTitle());
            message.setContent(notice.getNoticeContent());
            message.setUpdateBy(user);
            message.setUpdateTime(date);
            message.setReadTime(null);
            message.setReadStatus(MoeMessageStatus.UNREAD.getValue());
        });
        notice.setUpdateBy(user);
        notice.setUpdateTime(date);
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }
}
